package com.uduemc.biso.module.basic.service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.page.PageMethod;
import com.uduemc.biso.core.extities.center.Host;
import com.uduemc.biso.core.property.GlobalProperties;
import com.uduemc.biso.core.utils.NumberUUId;
import com.uduemc.biso.module.basic.service.mapper.HostMapper;
import com.uduemc.biso.module.basic.service.service.HostService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class HostServiceImpl implements HostService {

	@Autowired
	private HostMapper hostMapper;

	@Autowired
	private GlobalProperties globalProperties;

	@Override
	public Host findOne(Long id) {
		return hostMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Host> findByUserId(long userId) {
		Example example = new Example(Host.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", userId);
		List<Host> selectByExample = hostMapper.selectByExample(example);
		return selectByExample;
	}

	@Override
	public List<Host> findByUserId(Long userId, Short status) {
		return findByUserId(userId, status, globalProperties.getData().getPageSize());
	}

	@Override
	public List<Host> findByUserId(Long userId, Short status, Integer pageSize) {

		return findByUserId(userId, status, pageSize, globalProperties.getData().getPage());
	}

	@Override
	public List<Host> findByUserId(Long userId, Short status, Integer pageSize, Integer page) {
		Example example = new Example(Host.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", userId);
		criteria.andEqualTo("status", status);
		PageMethod.startPage(page, pageSize);
		List<Host> selectByExample = hostMapper.selectByExample(example);
		return selectByExample;
	}

	@Override
	public Host insert(Host host) {
		hostMapper.insert(host);
		return findOne(host.getId());
	}

	@Override
	public Host updateById(Host host) {
		hostMapper.updateByPrimaryKey(host);
		return findOne(host.getId());
	}

	@Override
	public Host insertSelective(Host host) {
		hostMapper.insertSelective(host);
		return findOne(host.getId());
	}

	@Override
	public Host updateByIdSelective(Host host) {
		hostMapper.updateByPrimaryKeySelective(host);
		return findOne(host.getId());
	}

	@Override
	public List<Host> findAll(Pageable pageable) {
		int pageNumber = pageable.getPageNumber();
		int pageSize = pageable.getPageSize();
		PageMethod.startPage(pageNumber, pageSize);
		return hostMapper.selectAll();
	}

	@Override
	public void deleteById(Long id) {
		hostMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Host findByRandomCode(String randomCode) {
		Example example = new Example(Host.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("randomCode", randomCode);
		return hostMapper.selectOneByExample(example);
	}

	@Override
	public String getCreateRandomCode(long userId) {
		String randomCode = NumberUUId.getNumberIdByUUId(userId);
		if (findByRandomCode(randomCode) == null) {
			return randomCode;
		}
		return getCreateRandomCode(userId);
	}

}
